Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums
    Public Class ForumsConfig
        Public sPath As String = HttpContext.Current.Server.MapPath("~/DesktopModules/ActiveForums/config/defaultsetup.config")
        Public Function ForumsInit(ByVal PortalId As Integer, ByVal ModuleId As Integer) As Boolean
            Try
                'Initial Settings
                LoadSettings(PortalId, ModuleId)
                'Add Default Templates
                LoadTemplates(PortalId, ModuleId)
                'Add Default Status
                LoadFilters(PortalId, ModuleId)
                'Add Default Steps
                LoadRanks(PortalId, ModuleId)
                'Add Default Forums
                LoadDefaultForums(PortalId, ModuleId)
                Return True
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
                Return False
            End Try
        End Function
        Private Sub LoadSettings(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            Try
                Dim objModules As New DotNetNuke.Entities.Modules.ModuleController
                Dim xDoc As New System.Xml.XmlDocument
                xDoc.Load(sPath)
                If Not xDoc Is Nothing Then

                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//mainsettings/setting")
                    If xNodeList.Count > 0 Then
                        Dim i As Integer = 0
                        For i = 0 To xNodeList.Count - 1
                            objModules.UpdateModuleSetting(ModuleId, xNodeList(i).Attributes("name").Value, xNodeList(i).Attributes("value").Value)

                        Next
                    End If
                End If
                objModules.UpdateModuleSetting(ModuleId, SettingKeys.IsInstalled, "True")
                objModules.UpdateModuleSetting(ModuleId, "NeedsConvert", "False")
                Try
                    Dim nfi As Globalization.DateTimeFormatInfo = New Globalization.CultureInfo("en-US", True).DateTimeFormat


                    objModules.UpdateModuleSetting(ModuleId, SettingKeys.InstallDate, Now().ToString(New Globalization.CultureInfo("en-US")))
                Catch ex As Exception
                    DotNetNuke.Services.Exceptions.LogException(ex)

                End Try


            Catch ex As Exception

            End Try
        End Sub
        Private Sub LoadTemplates(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            Try
                Dim xDoc As New System.Xml.XmlDocument
                xDoc.Load(sPath)
                If Not xDoc Is Nothing Then
                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//templates/template")
                    If xNodeList.Count > 0 Then
                        Dim tc As New TemplateController
                        Dim i As Integer = 0
                        For i = 0 To xNodeList.Count - 1
                            Dim ti As New TemplateInfo
                            With ti
                                .TemplateId = -1
                                .TemplateType = CType(xNodeList(i).Attributes("templatetype").Value, Templates.TemplateTypes)
                                .IsSystem = True
                                .PortalId = PortalId
                                .ModuleId = ModuleId
                                .Title = xNodeList(i).Attributes("templatetitle").Value
                                .Subject = xNodeList(i).Attributes("templatesubject").Value
                                Dim sHTML As String = String.Empty
                                sHTML = GetFileContent(xNodeList(i).Attributes("importfilehtml").Value)
                                Dim sText As String = String.Empty
                                sText = GetFileContent(xNodeList(i).Attributes("importfiletext").Value)
                                Dim sTemplate As String = sText
                                If Not sHTML = String.Empty Then
                                    sTemplate = "<template><html>" & HttpContext.Current.Server.HtmlEncode(sHTML) & "</html><plaintext>" & sText & "</plaintext></template>"
                                End If
                                .Template = sTemplate
                            End With
                            tc.Template_Save(ti)
                        Next
                    End If
                End If
            Catch ex As Exception
            End Try
        End Sub
        Private Sub LoadFilters(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            Utilities.ImportFilter(PortalId, ModuleId)
        End Sub
        Private Sub LoadRanks(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            Try
                Dim xDoc As New System.Xml.XmlDocument
                xDoc.Load(sPath)
                If Not xDoc Is Nothing Then

                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//ranks/rank")
                    If xNodeList.Count > 0 Then
                        Dim i As Integer = 0
                        For i = 0 To xNodeList.Count - 1
                            DataProvider.Instance.Ranks_Save(PortalId, ModuleId, -1, xNodeList(i).Attributes("rankname").Value, CInt(xNodeList(i).Attributes("rankmin").Value), CInt(xNodeList(i).Attributes("rankmax").Value), xNodeList(i).Attributes("rankimage").Value)
                        Next
                    End If
                End If
            Catch ex As Exception

            End Try
        End Sub
        Private Sub LoadDefaultForums(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            Dim xDoc As New System.Xml.XmlDocument
            xDoc.Load(sPath)
            If Not xDoc Is Nothing Then

                Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//defaultforums/groups/group")
                If xNodeList.Count > 0 Then
                    Dim i As Integer = 0
                    For i = 0 To xNodeList.Count - 1
                        Dim gi As New ForumGroupInfo
                        gi.ModuleId = ModuleId
                        gi.ForumGroupId = -1
                        gi.GroupName = xNodeList(i).Attributes("groupname").Value
                        gi.Active = CBool(xNodeList(i).Attributes("active").Value)
                        gi.Hidden = CBool(xNodeList(i).Attributes("hidden").Value)
                        gi.SortOrder = i
                        gi.GroupSettingsKey = ""
                        gi.PermissionsId = -1
                        Dim gc As New ForumGroupController
                        Dim groupId As Integer = -1
                        groupId = gc.Groups_Save(PortalId, gi, True)
                        gi = gc.GetForumGroup(ModuleId, groupId)
                        Dim sKey As String = "G:" & groupId.ToString
                        Dim sAllowHTML As String = "false"
                        If Not xNodeList(i).Attributes("allowhtml") Is Nothing Then
                            sAllowHTML = xNodeList(i).Attributes("allowhtml").Value
                        End If
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.TopicsTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.TopicTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EmailAddress, String.Empty)
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.UseFilter, "true")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowPostIcon, "true")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowEmoticons, "true")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowScript, "false")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.IndexContent, "false")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowRSS, "true")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowAttach, "true")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachCount, "3")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachMaxSize, "1000")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachTypeAllowed, ".jpg,.png,.gif,.zip")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachStore, "FILESYSTEM")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachMaxHeight, "450")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachMaxWidth, "450")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachUniqueFileNames, "false")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowHTML, sAllowHTML)
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorType, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorHeight, "350")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorWidth, "99%")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorToolbar, "bold,italic,underline,quote")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorStyle, "2")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.TopicFormId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ReplyFormId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.QuickReplyFormId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ProfileTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.IsModerated, "false")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.DefaultTrustValue, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AutoTrustLevel, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModApproveTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModRejectTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModMoveTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModDeleteTemplateId, "0")
                        DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModNotifyTemplateId, "0")
                        If Not groupId = -1 Then
                            If xNodeList(i).HasChildNodes Then
                                Dim cNodes As System.Xml.XmlNodeList = xNodeList(i).ChildNodes
                                For c As Integer = 0 To cNodes.Count - 1
                                    Dim fi As New Forum
                                    Dim fc As New ForumController
                                    fi.ForumID = -1
                                    fi.ModuleId = ModuleId
                                    fi.ForumGroupId = groupId
                                    fi.ParentForumId = 0
                                    fi.ForumName = cNodes(c).Attributes("forumname").Value
                                    fi.ForumDesc = cNodes(c).Attributes("forumdesc").Value
                                    fi.ForumSecurityKey = "G:" & groupId.ToString
                                    fi.ForumSettingsKey = "G:" & groupId.ToString
                                    fi.Active = CBool(cNodes(c).Attributes("active").Value)
                                    fi.Hidden = CBool(cNodes(c).Attributes("hidden").Value)
                                    fi.SortOrder = c
                                    fi.PermissionsId = gi.PermissionsId
                                    fc.Forums_Save(PortalId, fi, True, True)
                                Next
                            End If
                        End If
                    Next
                End If
            End If
        End Sub

        Private Function GetFileContent(ByVal FilePath As String) As String
            Dim sPath As String = HttpContext.Current.Server.MapPath(FilePath)
            Dim sContents As String = String.Empty
            Dim objStreamReader As IO.StreamReader
            If IO.File.Exists(sPath) Then
                Try
                    'objStreamReader = IO.File.OpenText(sPath)
                    objStreamReader = New IO.StreamReader(sPath, Text.Encoding.ASCII)
                    sContents = objStreamReader.ReadToEnd
                    objStreamReader.Close()
                Catch exc As Exception
                    sContents = exc.Message
                End Try
            End If
            Return sContents
        End Function

        Public Function CreateForumForGroup(PortalId As Integer, ModuleId As Integer, SocialGroupId As Integer, Config As String) As Integer
            Dim xDoc As New System.Xml.XmlDocument
            xDoc.LoadXml(Config)
            If Not xDoc Is Nothing Then
                Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                Dim xNodeList As System.Xml.XmlNodeList = xRoot.SelectNodes("//defaultforums/groups/group")
            End If
        End Function

    End Class
End Namespace

